<?php
namespace app\administrative\controller;
use think\Controller;
use think\Db;
use app\common\model\User;
use think\Cookie;
//use phpDocumentor\Reflection\Types\Null_;
/**
 *
 * @author 用户登录
 *
 */
class Login extends Controller{
    protected $db_name = 'administrative_user';
    protected $db_name_log = 'administrative_log';
    /**
     * 登录
     */
    public function index(){
        if(session($this->db_name.'uid')||session($this->db_name.'username')){
            $this->redirect('index/index');
        }
        return $this->fetch('/login');
    }

    /**
     * 登录验证
     */
    public function doLogin(){
        if(request()->isPost()){
            $username = input("param.account");
            $password = input("param.password");
            $code = input("param.code");
            $free_board = input("param.free_board");
            if(!captcha_check($code)){
                //验证失败
                return json(['code' => -2, 'url' => '', 'msg' => '验证码错误']);
            };

            //参数验证
            $result = $this->validate(compact('username', 'password'), 'AdminValidate');
            if(true !== $result){
                return json(['code' => -5, 'url' => '', 'msg' => $result]);
            }
            $hasUser = Db::name($this->db_name)->where('account', $username)->find();


            if(empty($hasUser)){
                return json(['code' => -1, 'url' => '', 'msg' => '管理员不存在']);
            }
            //验证密码
            if($hasUser['password'] !== md5(md5($password).$hasUser['strict'])){
                writelog($hasUser['id'],$username,'用户【'.$username.'】登录失败：密码错误',0,$this->db_name_log);
                return json(['code' => -2, 'url' => '', 'msg' => '账号或密码错误']);
            }
            writelog($hasUser['id'],$username,'用户【'.$username.'】登录失败：密码错误',0,$this->db_name_log);
            //验证帐号是否冻结
            if(1 != $hasUser['status']){
                writelog($hasUser['id'],$username,'用户【'.$username.'】登录失败：该账号被禁用',0,$this->db_name_log);
                return json(['code' => -6, 'url' => '', 'msg' => '该账号被禁用']);
            }

            if(0 < $hasUser['is_delete']){
                writelog($hasUser['id'],$username,'用户【'.$username.'】登录失败：该账号不存在',0,$this->db_name_log);
                return json(['code' => -6, 'url' => '', 'msg' => '管理员不存在']);
            }

            //获取模型
            $module     = strtolower(request()->module());
            $user = new User($this->db_name);
            //获取用户分组
            $info = $user->getRoleInfo($hasUser['groupid'],$module);
            //dump($info);exit;
            session($this->db_name.'uid', $hasUser['id']);         //用户ID
            session($this->db_name.'username', $hasUser['name']);  //用户名
            session($this->db_name.'portrait', $hasUser['avatar_img']); //用户头像
            session($this->db_name.'rolename', $info['title']);    //角色名
            session($this->db_name.'a_id', $hasUser['a_id']);      //行政中心ID
            session($this->db_name.'is_admin', $hasUser['is_admin']);
            //如果为超管登录 清除角色节点
            if(empty($info['rules'])){
                session($this->db_name.'rule', null);        //角色节点
            }else{
                session($this->db_name.'rule', $info['rules']);        //角色节点
            }
            
            if(!empty($info['name'])){
                session($this->db_name.'name', $info['name']);         //角色权限
            }
            $param = [
                'loginnum' => $hasUser['loginnum'] + 1,
                'last_login_ip' => request()->ip(),
                'last_login_time' => time()
            ];
            //设置3天免登
            if(!empty($free_board)){
                //设置登录cookie
                Cookie::set(strtolower(request()->module()).'_user',$hasUser,60*60*24*3);
            }
            


            Db::name($this->db_name)->where('id', $hasUser['id'])->update($param);
            writelog($hasUser['id'],session($this->db_name.'username'),'用户【'.session($this->db_name.'username').'】登录成功',1,$this->db_name_log);
            return json(['code' => 1, 'url' => url('Index/index'), 'msg' => '登录成功！']);

        }


    }

    //退出登录
    public function loginOut()
    {
        session($this->db_name.'uid', null);         //用户ID
        session($this->db_name.'username', null);  //用户名
        session($this->db_name.'portrait', null); //用户头像
        session($this->db_name.'rolename', null);    //角色名
        session($this->db_name.'rule', null);        //角色节点
        session($this->db_name.'a_id', null);
        session($this->db_name.'is_admin', null); //是否为超管
        Cookie::set(strtolower(request()->module()).'_user',null);
        return json(['code'=>1,'msg'=>'退出成功!','data'=>url('Login/index')]);
    }



}